package JDBC;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("182547");

        Connection connection = dataSource.getConnection();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请选择你要对数据库进行的操作:1.新增 2.修改 3.删除 4.查询");
            switch (sc.nextInt()) {
                case 1:
                    System.out.println("请输入你要新增学生的学生号：");
                    int insertId = sc.nextInt();
                    System.out.println("请输入你要新增学生的姓名：");
                    String insertName = sc.next();
                    SQLinsert insert = new SQLinsert();
                    insert.func(insertId, insertName, connection);
                    break;
                case 2:
                    System.out.println("请选择你要修改的内容：1.修改学生姓名 2.修改学生号");
                    switch (sc.nextInt()) {
                        case 1:
                            System.out.println("请输入你要修改学生对应的学生号");
                            int updateId = sc.nextInt();
                            System.out.println("请你输入修改后的学生姓名");
                            String newName = sc.next();
                            SQLupdate update1 = new SQLupdate();
                            update1.funcName(updateId, newName, connection);
                            break;
                        case 2:
                            System.out.println("请输入你要修改学生对应的学生姓名");
                            String updateName = sc.next();
                            System.out.println("请你输入修改后的学生姓名");
                            int newId = sc.nextInt();
                            SQLupdate update2 = new SQLupdate();
                            update2.funcId(newId, updateName, connection);
                            break;
                        default:
                            System.out.println("操作无效");
                            break;
                    }
                case 3:
                    System.out.println("请输入你要删除学生的姓名");
                    String deleteName = sc.next();
                    SQLdelete delete=new SQLdelete();
                    delete.func(deleteName,connection);
                    break;
                case 4:
                    System.out.println("请输入你要查询的学生姓名");
                    String selectName = sc.next();
                    SQLselect select =new SQLselect();
                    select.func(selectName,connection);
                    break;
                default:
                    System.out.println("此操作无效");
                    break ;
            }
        }
    }
}
